<?php
namespace app\lnadmin\controller;
use app\core\InitController;
use think\facade\View;
use app\lnadmin\model\admin\Navbar as NavbarModel;

class Navbar extends InitController
{
    // 列表
    public function list()
    {

        return View::fetch('navbar');
    }
    // 数据
     public function index_json()
    {
            $tice = new NavbarModel();
        if ($this->request->isAjax()) {
            $count = $tice->count();
            $list  = $tice->select();
            $pidMenuList = $this->buildPidMenu(0, $list);
            $data  = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $pidMenuList,
            ];
            return json($data);
        }
       
    }
 public function buildPidMenu($pid, $list, $level = 0)
    {
        $newList = [];
        foreach ($list as $vo) {
            if ($vo['pid'] == $pid) {
                $level++;
                foreach ($newList as $v) {
                    if ($vo['pid'] == $v['pid'] && isset($v['level'])) {
                        $level = $v['level'];
                        break;
                    }
                }
                $vo['level'] = $level;
                if ($level > 1) {
                    $repeatString = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    $markString   = str_repeat("{$repeatString}├{$repeatString}", $level - 1);
                    $vo['title']  = $markString . $vo['title'];
                }
                $newList[] = $vo;
                $childList = $this->buildPidMenu($vo['id'], $list, $level);
                !empty($childList) && $newList = array_merge($newList, $childList);
            }

        }
        return $newList;
    }
    // 首页分类新增
    public function add()
    { 
        if ($this->request->isPost()) {
            $param = $this->request->param();
                 //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'navbar');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $param['type'] = NavbarModel::TYPE_INDEX;  //首页分类
            $result = NavbarModel::create($param); 
            if ($result == true) {
            add_admin_log('add', '首页分类');// 记录行为日志
         $this->success(true,0,'新增成功');
            } else {
              $this->error('新增失败');
            }
        }
       
        $authRule = NavbarModel::order('weigh asc')->select();
        return $this->fetch('saveRule', ['authRule' => list_to_level($authRule)]);
    }

    // 编辑
    public function edit()
    {
        if ($this->request->isPost()) {
             $param = $this->request->param();
                 //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'Navbar');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
        $edit = NavbarModel::update($param,['id'=>$param['id']]);
            add_admin_log('edit', '首页分类');// 记录行为日志
        return $edit ? $this->saveSuccess() : $this->error();
    }
      $authRule = NavbarModel::order('weigh asc')->select();
        return $this->fetch('saveRule', [
            'data' => NavbarModel::where('id', input('id'))->find(),
            'authRule' => list_to_level($authRule)
        ]);
    }
    /**
     * 删除
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $result = NavbarModel::destroy($param['id']);
            if ($result == true) {
            add_admin_log('del', '首页分类');// 记录行为日志
                return $this->success('', '', '删除成功');
            }
        }
    }
}